package com.yb.designpattern.creation.singleton;

/**
 * 懒汉式
 * 若不加同步线程不安全
 * @author Administrator
 *
 */
//public class LazySingleton {
//	private static LazySingleton instance;
//	private LazySingleton(){}
//
//	//方法加锁，线程安全，但是性能不够好
//	public static synchronized LazySingleton getInstance(){
//		if (null == instance) {
//			instance = new LazySingleton();
//		}
//		return instance;
//	}
//}

//双重加锁
public class LazySingleton {
    private static LazySingleton instance = null;
    private LazySingleton(){}
    public static LazySingleton getInstance() {
        if (instance == null) {
            synchronized (LazySingleton.class) {
                if (instance == null) {//2
                    instance = new LazySingleton();
                }
            }
        }
        return instance;
    }
}

//双重加锁
//public class LazySingleton {  
//  private static LazySingleton instance = null;  
//  private LazySingleton(){}  
//  public static LazySingleton getInstance() {  
//      if (instance == null) {  
//          synchronized (LazySingleton.class) {  
//              if (instance == null) {//2  
//                  instance = new LazySingleton();  
//              }  
//          }  
//      }  
//      return instance;  
//  }  
//} 
